﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using I = IML.Shared;
using System.Drawing;

namespace IML
{
    public static class USFlag
    {
        public static I.ImageCompilerRequest Request(int CanvasWidth, int CanvasHeight)
        {
            I.ImageCompilerRequest request = new I.ImageCompilerRequest()
            {
                CanvasWidth = CanvasWidth,
                CanvasHeight = CanvasHeight,
                InitialBrush = new I.Brush() { R = 255, G = 255, B = 255 }
            };

            // @@Red
//            request.Invocations.Add(new I.BrushInvocation() { Brush = new I.Brush() { R = 255, G = 0, B = 0 } });
            request.Invocations.Add(new I.BrushInvocation() { Brush = new I.Brush() { FillColor = ColorTranslator.FromHtml("red") } });

            //@Stripe = @Polygon([0,2/13 : 1,0 : 1, 1/13 : 0, 1/13])
            request.ShapeDeclarations.Add(new KeyValuePair<string, I.NestedShape>("RedStripe", new I.Polygon()
            {
                PointsFunction = new I.PointList()
                {
                    Verticies = new I.XYAttribute[]{new I.XYAttribute{X = 0, Y = 0}, 
                                                        new I.XYAttribute{X = 1, Y = 0},
                                                        new I.XYAttribute{X = 1, Y = 1f/13},
                                                        new I.XYAttribute{X = 0, Y = 1f/13}}
                }
            }));

            //@Stripe * PPLine(.5,0; .5, 14/13; 7, 1/28)
            request.Invocations.Add(new I.Repeater()
            {
                VariableName = "RedStripe",
                PointFunction = new I.LineFunction(7, 1f / 28)
                {
                    Start = new I.XYAttribute() { X = .5f, Y = 0 },
                    End = new I.XYAttribute() { X = .5f, Y = 14f / 13 },
                }
            });

            //@@Blue
            request.Invocations.Add(new I.BrushInvocation() { Brush = new I.Brush() { R = 0, G = 0, B = 255 } });

            //Polygon([0, 0 : 0.45, 0 : 7/13 , 0.45 : 7/13, 0.0])
            request.Invocations.Add(new I.ShapeInvocation()
            {
                Shape = new I.Polygon()
                {
                    PointsFunction = new I.PointList()
                    {
                        Verticies = new I.XYAttribute[]{new I.XYAttribute{X = 0, Y = 0}, 
                                                        new I.XYAttribute{X = 0.45f, Y = 0},
                                                        new I.XYAttribute{X = 0.45f, Y = 7f/13},
                                                        new I.XYAttribute{X = 0, Y = 7f/13}}
                    }
                }
            });

            var blueRect = (request.Invocations[request.Invocations.Count - 1] as I.ShapeInvocation).Shape as I.Polygon;

            //Polygon(Ellipses([(.45, .45) : (.25 , .25)]; .5, .5; 10; -1/4)
            blueRect.ShapeDeclarations.Add(new KeyValuePair<String, I.NestedShape>("Star", new I.Polygon()
            {
                PointsFunction = new I.EllipseFunction(10, -.25f)
                {
                    Center = new I.XYAttribute() { X = .5f, Y = .54f },
                    Radiuses = new I.XYAttribute[]{new I.XYAttribute{X = .5f, Y = .5F, MaintainAspectRatio = true}, 
                                                   new I.XYAttribute{X = .2f, Y = .2F, MaintainAspectRatio = true}},
                }
            }));

            var starBox = new I.Polygon()
            {
                PointsFunction = new I.PointList()
                {
                    Verticies = new I.XYAttribute[]{new I.XYAttribute{X = 0, Y = 0, MaintainAspectRatio = true}, 
                                                    new I.XYAttribute{X = .9f, Y = 0, MaintainAspectRatio = true},
                                                    new I.XYAttribute{X = .9f, Y = .9f, MaintainAspectRatio = true},
                                                    new I.XYAttribute{X = 0, Y = .9f, MaintainAspectRatio = true}}
                }
            };

            //@@White
            starBox.Invocations.Add(new I.BrushInvocation() { Brush = new I.Brush() { R = 255, G = 255, B = 255 } });
            starBox.Invocations.Add(new I.ShapeVariableInvocation() { VariableName = "Star" });
            blueRect.ShapeDeclarations.Add(new KeyValuePair<string, I.NestedShape>("starBox", starBox));


            var sixStarStripe = new I.Polygon()
            {
                PointsFunction = new I.PointList()
                {
                    Verticies = new I.XYAttribute[]{new I.XYAttribute{X = 0, Y = 0}, 
                                                    new I.XYAttribute{X = 1, Y = 0},
                                                    new I.XYAttribute{X = 1, Y = 1f/9},
                                                    new I.XYAttribute{X = 0, Y = 1f/9}}
                }
            };

            sixStarStripe.Invocations.Add(new I.Repeater()
            {
                VariableName = "starBox",
                PointFunction = new I.LineFunction(6, 1f / 12)
                {
                    Start = new I.XYAttribute { X = 0, Y = .5f },
                    End = new I.XYAttribute { X = 1, Y = .5f }
                }
            });

            blueRect.ShapeDeclarations.Add(new KeyValuePair<string, I.NestedShape>("6StarStripe", sixStarStripe));
            blueRect.Invocations.Add(new I.Repeater()
            {
                VariableName = "6StarStripe",
                PointFunction = new I.LineFunction(5, 1f / 20)
                {
                    Start = new I.XYAttribute { X = .5f, Y = 0 },
                    End = new I.XYAttribute { X = .5f, Y = 10f / 9 }
                }
            });

            var fiveStarStripe = new I.Polygon()
            {
                PointsFunction = new I.PointList()
                {
                    Verticies = new I.XYAttribute[]{new I.XYAttribute{X = 0, Y = 0}, 
                                                    new I.XYAttribute{X = 1, Y = 0},
                                                    new I.XYAttribute{X = 1, Y = 1f/9},
                                                    new I.XYAttribute{X = 0, Y = 1f/9}}
                }
            };

            fiveStarStripe.Invocations.Add(new I.Repeater()
            {
                VariableName = "starBox",
                PointFunction = new I.LineFunction(5, 1f / 10)
                {
                    Start = new I.XYAttribute { X = 1f / 12, Y = .5f },
                    End = new I.XYAttribute { X = 11f / 12, Y = .5f }
                }
            });

            blueRect.ShapeDeclarations.Add(new KeyValuePair<string, I.NestedShape>("5StarStripe", fiveStarStripe));
            blueRect.Invocations.Add(new I.Repeater()
            {
                VariableName = "5StarStripe",
                PointFunction = new I.LineFunction(4, 0)
                {
                    Start = new I.XYAttribute { X = .5f, Y = 3f / 18 },
                    End = new I.XYAttribute { X = .5f, Y = 19f / 18 }
                }
            });
            return request;
        }
    }
}
